// 引入必要的模块
import express from 'express';
import bodyParser from 'body-parser';
import {v4 as uuidv4} from 'uuid';


app.use(cors());

// 你的其他路由和中间件代码

app.listen(8180, () => {
    console.log('Server is running on port 8180');
});

// 创建 Express 应用
const app = express();
const PORT = 3000;

// 使用 JSON 解析中间件
app.use(bodyParser.json());

// 模拟数据库，存储挂号信息
let registrations = [];

// 获取所有挂号信息
app.get('/registration/findall', (req, res) => {
    res.json(registration);
});

// 创建挂号信息
app.post('/registration', (req, res) => {
    const newRegistration = req.body;
    newRegistration.re_id = uuidv4(); // 为新挂号信息生成唯一ID
    registrations.push(newRegistration);
    res.status(201).json(newRegistration);
});

// 更新挂号信息
app.put('/registration/:id', (req, res) => {
    const id = req.params.id;
    const updatedRegistration = req.body;
    registrations = registrations.map(registration => {
        if (registration.re_id === id) {
            return {...registration, ...updatedRegistration};
        }
        return registration;
    });
    res.json(updatedRegistration);
});

// 删除挂号信息
app.delete('/registrations/:id', (req, res) => {
    const id = req.params.id;
    registrations = registrations.filter(registration => registration.re_id !== id);
    res.sendStatus(204);
});

// 启动服务器
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});